//
//  JFBOrderGoodsDS.m
//  ios-supplier
//
//  Created by Benlai on 15/8/17.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBOrderGoodsMenuDS.h"

@implementation JFBOrderGoodsMenuDS
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.ID = [[attributes objectForKey: @"id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"id"] integerValue];

    self.name = [[attributes objectForKey: @"name"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"name"];
    self.cateTreeId = [[attributes objectForKey: @"cateTreeId"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"cateTreeId"] integerValue];
    
    return self;
}

+ (NSURLSessionDataTask *)loadMenuList:(void (^)(NSArray *menuList, NSError *error))block pager:(JFBPager*)pager
{
    NSDictionary * parameters = @{@"ut":[UserInfo currentUser].ut,
                                  @"merchantId":[UserInfo currentUser].merchantID,
                                  @"companyId":@(4),
                                  @"categoryTreeNodeId":@""
                                  };
    
    return [[JFBAPIClient sharedClient:false] GET: @"/opms-api/queryCategoryTreeNode.do" parameters: parameters success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"code"];
        NSLog(@"JSON ===== %@", JSON);
        if( [error isEqual:@"0"]) {
            NSMutableArray *result = [@[] mutableCopy];
            NSArray *loginResponse = [JSON valueForKeyPath:@"data"];
            for( NSDictionary *staff in loginResponse ) {
                JFBOrderGoodsMenuDS * ds = [[JFBOrderGoodsMenuDS alloc] initWithAttributes:staff];
                [result addObject:ds];
            }
            block([NSArray arrayWithArray:result], nil);
        }else if ([JSON[@"code"] integerValue] == 99){
            UIViewController* viewController;
            UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            viewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Login"];
        }else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
    
}
@end
